var time=0;
$(function(){
    getbackups();

    function creatDiv(i,o){
   var div= "<div class='forum-item'>"+
            "<div class='row'> <div class='col-sm-9'><div class='forum-icon'>" +
            "<i class='fa fa-database' style='color: #00a4a2'></i>"+
            "</div> <a class='forum-item-title'>"+(i+1)+"--->"+o.name+"</a>"+
            "<div class='forum-sub-title'>"+o.time+"</div></div>"+
            "<div class='col-sm-1 forum-info'><span class='views-number'>"+
            "<button opa='ipt' filepath='"+o.filepath+"' class='btn btn-success  dim' type='button'><i class='fa fa-upload'></i></button></span> <div>"+
            "<small>覆盖数据库</small></div></div><div class='col-sm-1 forum-info'>"+
            "<span class='views-number'>"+
            "<button opa='load' filepath='"+o.filepath+"' class='btn btn-success  dim' type='button'><i class='fa fa-download'></i></button></span>"+
            "<div><small class='Point'>下载备份</small></div></div>" +
       "<div class='col-sm-1 forum-info'><span class='views-number'>"+
        "<button opa='del' filepath='"+o.filepath+"' class='btn btn-danger dim' type='button'><i class='fa fa-trash-o'></i></button></span> <div>"+
        "<small>删除备份</small></div></div>" +
       "</div></div>";

    $("#datalist").append(div);
    }
/*
* 下载
* */
    $("#datalist").on("click","button",function(){

        if($(this).attr("opa")=="load"){  //下载按钮
            var url=$("#path").val()+"/loadbackups?filepath="+$(this).attr("filepath");
            window.location.href=url;

        }
        else if($(this).attr("opa")=="ipt"){
            var filepath=$(this).attr("filepath");
            swal({
                    title: "覆盖正在使用数据库",
                    text: "请谨慎操作,可能会造成严重后果!",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确认覆盖",
                    closeOnConfirm: false
                },
                function(){
                    var url=$("#path").val()+"/usebackups?filepath="+filepath;
                    $.get(url,function(json){
                        var data=JSON.parse(json);
                        swal(data.msg,"",data.status);
                    })
                });



        }

        else if($(this).attr("opa")=="del"){
            var filepath=$(this).attr("filepath");
            swal({
                    title: "确认删除？",
                    text: "请谨慎操作,删除后无法复原!",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确认删除",
                    closeOnConfirm: false
                },
                function(){
                    var url=$("#path").val()+"/delbackups?filepath="+filepath;
                    $.get(url,function(json){
            try{
                        var data=JSON.parse(json);
                        swal(data.msg,"",data.status)
                        $("#datalist").text("");
                        $("#time").text("")
                        getbackups();
            }catch (e){

            }
                    })

                });



        }

    });

/*备份*/
$("#backups").click(function(){
    $.get($("#path").val()+"/backups",function(json){
    var data=JSON.parse(json);
        swal(data.opare,data.msg,data.status);
        if(data.status=="success"){
             time=0;
            countdown();
            setTimeout(function(){
                $("#datalist").text("");
                $("#time").text("")
                getbackups();
            },10000);
        }
    });
});


    function countdown() {
        if(time>9){
            return false;
        }
        else{
            $("#time").text("----正在备份数据-->"+time+"s")
             setTimeout(countdown, 1000);
            time++;
        }

    }

function getbackups(){
    $.get($("#path").val()+"/getbackups",function(json){
        if(json!=null){
            var datalist=JSON.parse(json);
            if(datalist!=null && datalist.length>0){
                // console.info(datalist)
                $("#total").text("版本数: "+datalist.length);
                datalist.reverse();
                $.each(datalist,function(i,o){
                    creatDiv(i,o)
                });

            }
            else{
                $("#total").text("版本数: "+0);
            }
        }
        else{
            $("#total").text("版本数: "+0);


        }


    })
}


});